package com.astrob.activitys;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.astrob.NavFrameSDK.NavPosition;
import com.astrob.naviframe.Start;

public class NMEALogProvider {

	private static String NMEALOGPATH = "";
	private static NMEALogProvider instance = new NMEALogProvider();
	private File mLogFile = null;
	private BufferedReader mReadBuffer = null;
	private String mReadLine = null;

	public static NMEALogProvider getInstance() {
		return instance;
	}

	public NMEALogProvider() {
		NMEALOGPATH = Start.RUNDIR + "/NMEALog.dat";

		mHaveNMEALog = open();
	}

	private boolean mHaveNMEALog = false;

	public boolean haveNMEALog() {
		return mHaveNMEALog;
	}

	private boolean open() {
		mLogFile = new File(NMEALOGPATH);
		if (!mLogFile.exists() && !mLogFile.canRead())
			return false;

		try {
			mReadBuffer = new BufferedReader(new FileReader(mLogFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return true;
	}

	int num = 0;

	public double _alt = 0;

	public boolean getNextData(NavPosition navpos) {
		if (mReadBuffer == null)
			return false;
		try {
			String[] wordArray;
			while (true) {
				try {
					mReadLine = mReadBuffer.readLine();
					if (mReadLine == null) {
						mReadBuffer.reset();
						continue;
					}
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}

				if (mReadLine.length() < 8) {
					continue;
				}

				num++;

			/*
			 * if(num < 2500) continue;
			 */
				navpos.signal = 0;
				wordArray = mReadLine.split(",");
				if (wordArray[0].equalsIgnoreCase("$GPGSA")) {
					String strPDOP = wordArray[15];
					double dPDOP = Double.valueOf(strPDOP);
					navpos.signal = (int) (dPDOP * 3);
				}
				if (wordArray[0].equalsIgnoreCase("$GPGGA")) {
					_alt = Double.valueOf(wordArray[9]);
				}
				if (wordArray[0].equalsIgnoreCase("$GPRMC"))
					break;
			}

			if (wordArray[2].equalsIgnoreCase("A")) {
				// navpos.signal = 0;
			} else {
				navpos.signal = 100;
			}

			String strLat = wordArray[3];
			if (strLat.length() < 1)
				return false;

			int lat0 = (int) (Double.valueOf(strLat) / 100);
			double lat1 = Double.valueOf(strLat) - lat0 * 100;
			navpos.pos.lat = lat0 + (lat1 / 60);

			if (wordArray[4].equalsIgnoreCase("S")) {
				navpos.pos.lat = -navpos.pos.lat;
			}

			String strLon = wordArray[5];
			if (strLon.length() < 1)
				return false;
			int lon0 = (int) (Double.valueOf(strLon) / 100);
			double lon1 = Double.valueOf(strLon) - lon0 * 100;
			navpos.pos.lon = lon0 + (lon1 / 60);

			if (wordArray[6].equalsIgnoreCase("W")) {
				navpos.pos.lon = -navpos.pos.lon;
			}

			String strSpeed = wordArray[7];
			if (strSpeed.length() > 0) {
				navpos.vel = Double.valueOf(strSpeed) * 1.852;
			}

			String strHeading = wordArray[8];
			if (strHeading.length() > 0) {
				navpos.dir = Double.valueOf(strHeading);
			}
		} catch (NumberFormatException e) {
			return false;
		}

		// NavFrameSDK.DecodeLonLat(navpos.pos);

		return true;
	}

}
